package com.vandzi.android.androidcode.model;

import java.util.Comparator;

public class ApplicationsComparator implements Comparator<Application> {

	/**
	 * condition - date when application was logged in competition
	 */
	public final static int SORT_NEWEST = 1;
	/**
	 * condition - score(number of votes x rating)
	 */
	public final static int SORT_MOST_POPULAR = 2;

	/**
	 * condition - number of stars - rating attribute
	 */
	public final static int SORT_STARS = 3;

	private int condition = SORT_NEWEST;

	/**
	 * 
	 * @param sortingContidion
	 *            - ApplicationsComparator.SORT_*
	 */
	public ApplicationsComparator(int sortingContidion) {
		this.condition = sortingContidion;
	}

	@Override
	public int compare(Application lhs, Application rhs) {

		// first is less -1,equal 0 ,greater 1

		int result = 0;

		switch (condition) {
		case SORT_NEWEST:
			if (lhs.getOrder() <= rhs.getOrder()) {
				result = -1;
			} else {
				result = 1;
			}
			break;
		case SORT_MOST_POPULAR:
			if (lhs.getScore() <= rhs.getScore()) {
				result = 1;
			} else {
				result = -1;
			}

			break;
		case SORT_STARS:
			if (lhs.getAverageRating() <= rhs.getAverageRating()) {
				result = 1;
			} else {
				result = -1;
			}
			break;
		default:
			break;
		}

		return result;
	}

}
